package ControlLayer;
import java.util.ArrayList;

import ModelLayer.*;
//model layer package has to be imported
/**
 * Controller class for the DVD collection
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * @revised version English (Ab)
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class DVDCtr
{
    //declare instance variables to reference containers
    private static ArrayList<DVD> collectionDVD;
    
    /**
     * Constructor for objects of class DVDCtr
     */
    public DVDCtr()
    {
        //set up references to containers 
    	collectionDVD = new ArrayList<DVD>();
        
    }
    //Dummy implementation: after implementing the model layer 
    //objects of class DVD are to be returned, not String
    public static DVD getDvd(long _id)
    {
        DVD d = new DVD();
        d = null;
        for (int i=0; i<collectionDVD.size(); i++)
        {
            if (collectionDVD.get(i).getId() == _id)
            { 
                d = collectionDVD.get(i);
                return d;
            }
            else
                d = null;
        }
        
        return d;
    }
    //Dummy implementation: more parameters are probaly to added
    public static void createDvd(DVD _d)
    {       
          //this method constructs a new dvd object 
          //and stores it in the container
              collectionDVD.add(_d);
    }
    
    public static void deleteDvd(long _id)
    {
        //search for the person and delete the object from the container
        if (DVDCtr.getDvd(_id) != null)
        {
            for (int i=0; i<collectionDVD.size(); i++)
            {
                if (collectionDVD.get(i).getId() == _id)
                {
                    collectionDVD.remove(i);
                }
            }
        }
        else
            System.out.println("The DVD is not present in the DVD collection");
    }
    
    public static void createCopy(long _id, long _serialNumber, String _purchaseDate, double _purchasePrice, boolean _availability)
    {
        //create a copy of the specified dvd and add it to the dvd
        for (int i=0; i<collectionDVD.size(); i++)
        {
            if (collectionDVD.get(i).getId() == _id)
            {
                Copy c = new Copy();
                c.setSerialNumber(_serialNumber);
                c.setPurchaseDate(_purchaseDate);
                c.setPurchasePrice(_purchasePrice);
                c.setAvailability(_availability);
                collectionDVD.get(i).addCopy(c);
            }
        }
    }
    
    public static Copy getCopy(long _id, long _serialNumber)
    {
        return getDvd(_id).getCopy(_serialNumber);
    }
    
    public static void deleteCopy(long _id, long _serialNumber)
    {
        getDvd(_id).deleteCopy(_serialNumber);
    }
        
    public static ArrayList<DVD> listAllDvd()
    {
          //returns a list of all dvds and their associated copies
          return collectionDVD;
    }
    
    public ArrayList<Copy> listAllCopies(long _id)
    {
        return getDvd(_id).listAllCopies();
    }
    
    public static void updateCopy(long _id, Copy c)
    {
        getDvd(_id).updateCopy(c);
    }
    
    public void updateDvd(DVD _dvd)
    {
       getDvd(_dvd.getId()).setTitle(_dvd.getTitle());
       getDvd(_dvd.getId()).setArtist(_dvd.getArtist());
       getDvd(_dvd.getId()).setPublicationDate(_dvd.getPublicationDate());
    }
}
